/**
 * 
 */
package org.vsg.stock.extension.processor;

import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import java.util.Vector;

import org.vsg.stock.basic.domain.StockDailyLine;
import org.vsg.stock.extension.vo.AnalysisKLineUnitsVO;
import org.vsg.stock.extension.vo.ExtStockDailyLineVO;

/**
 * @author vison
 *
 */
public class KLineHelperManager {
	
	public static final List<ExtStockDailyLineVO> convertExtStkDailyLineVOList(List<StockDailyLine> stockDailyLineList) {
		List<ExtStockDailyLineVO> allLines = new Vector<ExtStockDailyLineVO>();
		for (Iterator<StockDailyLine> iterLine = stockDailyLineList.iterator() ; iterLine.hasNext();) {
			allLines.add( new ExtStockDailyLineVO( iterLine.next() ) );
		}
		return allLines;
	}
	
	public static final List<AnalysisKLineUnitsVO> detachAnalysisKLineUnits(List<ExtStockDailyLineVO> extStkDailyLineVOList) {
		Set<Integer> positionIndex = new HashSet<Integer>();
		ExtStockDailyLineVO extStkDailyLineVO = null;
		
		// --- check the add stk daily ---
		for (int i = 0 ; i < extStkDailyLineVOList.size() ;i++) {
			extStkDailyLineVO = extStkDailyLineVOList.get(i);
			if (Math.abs( extStkDailyLineVO.getEntityRatio() ) > 2.5) {
				if (i+1 < extStkDailyLineVOList.size() ) {
					if (Math.abs( extStkDailyLineVOList.get(i+1).getEntityRatio() ) <= 2.5  ) {
						positionIndex.add( i );
					}
				} else {
					positionIndex.add( i );
				}
			}
		}
		
		// --- the second scan ----
		boolean begin = false;
		for (int i = 0 ; i < extStkDailyLineVOList.size() ;i++) {
			extStkDailyLineVO = extStkDailyLineVOList.get(i);
			if (Math.abs( extStkDailyLineVO.getEntityRatio() ) > 2.5) {
				if (i+1 < extStkDailyLineVOList.size() ) {
					if (begin && Math.abs( extStkDailyLineVOList.get(i+1).getEntityRatio() ) <= 2.5  ) {
						begin = false;
						positionIndex.add(i);
					} else if(!begin && Math.abs( extStkDailyLineVOList.get(i+1).getEntityRatio() ) > 2.5 ) {
						begin = true;
						positionIndex.add(i);
					}
				}
			}
		}
		
		// ---- sort object ---
		List<Integer> orderPositionIndex = new Vector(positionIndex);
		Collections.sort( orderPositionIndex );
		Collections.reverse( orderPositionIndex );
		
		List<AnalysisKLineUnitsVO> anaKLineUnitsVO = new LinkedList<AnalysisKLineUnitsVO>();
		
		int end = extStkDailyLineVOList.size();
		LinkedList<ExtStockDailyLineVO> unitsList = null;
		List<ExtStockDailyLineVO> anotherUnitsList = null;
		for (int i = 0 ;i < orderPositionIndex.size() ;i++) {
			unitsList = new LinkedList<ExtStockDailyLineVO>();
			if (i>0) {
				end = orderPositionIndex.get(i-1);
			}
			for (int j = end-1  ; j >orderPositionIndex.get(i) ;j--) {
				unitsList.add( extStkDailyLineVOList.get(j) );
			}
			anotherUnitsList = (List<ExtStockDailyLineVO>)unitsList.clone();
			anotherUnitsList.add( extStkDailyLineVOList.get(orderPositionIndex.get(i)) );
			
			// ---- add the obejct reference ---
			AnalysisKLineUnitsVO akuVO = new AnalysisKLineUnitsVO();
			akuVO.setStartDate(  extStkDailyLineVOList.get( orderPositionIndex.get(i) ).getDate() );
			akuVO.setExtStkDailLineVOList( unitsList );
			anaKLineUnitsVO.add( akuVO );

			akuVO = new AnalysisKLineUnitsVO();
			akuVO.setStartDate(  extStkDailyLineVOList.get( orderPositionIndex.get(i) ).getDate() );
			akuVO.setExtStkDailLineVOList( anotherUnitsList );
			anaKLineUnitsVO.add( akuVO );			
			
		}

		
		return anaKLineUnitsVO;
	}
	

}
